package cn.lysj.framework.common.utils.string;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 该类提供多个主键ID与字符串的相互转化
 * 
 * @author LinLion
 * 
 */
public class LongArrayStringUtil {

	/**
	 * 将Long[]数组转化为String,并用'-'隔开
	 * 
	 * @param array:Long[]数组 : [123, 124, 225]
	 * @return string字符串:'-123-124-225-'
	 */
	public static String longArray2String(Long[] array) {
		if (array == null || array.length == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder("-");
		for (Long l : array) {
			sb.append(String.valueOf(l));
			sb.append("-");
		}
		return sb.toString();
	}
    /**
     * String[]数组转化为String,并用'-'隔开
     *
     * @param array:Long[]数组 : [123, 124, 225]
     * @return string字符串:'-123-124-225-'
     */
    public static String stringArray2String(String[] array) {
        if (array == null || array.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder("-");
        for (String l : array) {
            sb.append(String.valueOf(l));
            sb.append("-");
        }
        return sb.toString();
    }

	/**
	 * 将String转化为Long[]数组,要求字符串用'-'隔开
	 * 
	 * @param string:字符串:'-123-124-225-'
	 * @return Long数组[123, 124, 225]
	 */
	public static Long[] string2LongArray(String string) {
		if (string == null || string.trim().equals("")) {
			return new Long[0];
		}
		String[] stringArr = string.split("-");
		List<Long> longList = new ArrayList<>();
		for (String s : stringArr) {
			if (s == null || s.equals("")) {
				continue;
			}
			longList.add(Long.parseLong(s));
		}

		Long[] longArr = longList.toArray(new Long[longList.size()]);
		return longArr;
	}

}
